package org.e_gold.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;
import org.e_gold.dao.ContractDAO;
import org.e_gold.dao.SettledContractDAO;
import org.e_gold.factory.AppProperties;
import org.e_gold.model.Contract;
import org.e_gold.model.SettledContract;
import org.e_gold.service.AIService;
import org.e_gold.service.BrowserService;
import org.e_gold.service.ContractService;
import org.e_gold.service.UserAccountService;
import org.e_gold.util.CalcUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AIServiceImpl implements AIService {

    private static final Logger logger = Logger.getLogger(AIServiceImpl.class);
    @Autowired
    private UserAccountService userAccountService;

    @Autowired
    private SettledContractDAO settledContractDAO;

    @Autowired
    private BrowserService browserService;

    @Autowired
    private AppProperties appProperties;

    @Autowired
    private ContractService contractService;

    @Autowired
    private ContractDAO contractDAO;

    @Override
    public List<Contract> getQualifyContract(List<Contract> allContracts) {

        if (allContracts != null && allContracts.size() > 0) {
            // update all current contract to false qualify
            contractService.updateFalseQualify();
            // sort play time
            Collections.sort(allContracts);
            List<Contract> qualifyContracts = new ArrayList<Contract>();
            for (Contract c : allContracts) {
                if (c.getContractType().indexOf("Under") > -1) { // under
                    Float rate = c.getRate();
                    if (appProperties.getRateMin() <= rate && rate <= appProperties.getRateMax()) {
                        c.setQualify(true);
                        contractDAO.update(c);
                        qualifyContracts.add(c);
                    }
                }
            }
            if (qualifyContracts.size() > 0) {
                return qualifyContracts;
            }
        }
        return null;
    }

    @Override
    public void virtualPlaceBet(List<Contract> qualifyContract) {

        try {
            for (Contract bestContract : qualifyContract) {
                logger.info("::::::::::::::::::::::::::::::::::::::::::::");
                logger.info("@@ Virtual place bet for best event : " + bestContract.getEvent().getName());
                logger.info("@@ best contract : " + bestContract.getContractType());
                logger.info("@@ best rate : " + bestContract.getRate());
                logger.info("@@ current play time : " + bestContract.getEvent().getPlayTime());
                logger.info("@@ current goal : " + bestContract.getEvent().getTotalGoal());
                logger.info("::::::::::::::::::::::::::::::::::::::::::::");
                settledContractDAO.updateAllRunningToFalse();
                Float balance = userAccountService.getRealBalance();
                logger.info("Roll total balance" + balance);
                if (balance != 0f) {
                    if (browserService.placeBet(bestContract)) {
                        SettledContract settledContract = new SettledContract();
                        settledContract.setContract(bestContract);
                        settledContract.setBetAmount(balance);
                        settledContract.setWonAmount(balance * bestContract.getRate());
                        settledContract.setResultAmount(0F);
                        settledContract.setRunning(true);
                        settledContract.setUnderGoal(CalcUtil.getUnderTotalGoal(bestContract.getContractType()));
                        settledContractDAO.insert(settledContract);
                        // stop loop
                        break;
                    }
                }
            }
        } catch (Exception e) {
            logger.error(" virtualPlaceBet error");
            logger.error(e);
        }
    }

    @Override
    public Boolean calcResult(SettledContract running) {

        running.setWon(running.getCurrentGoal() < running.getUnderGoal());
        settledContractDAO.update(running);
        return running.getWon();
    }
}
